<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        span{
            font-size: 12px;
            color: red;
        }
    </style>
</head>
<body>
<form action="">
    <input type="text" name="username" @blur="blurfn()" placeholder="用户名">
    <span>{{info}}</span><br>
    <input type="password" name="password" placeholder="密码"><br>
    <input type="text" name="nick" placeholder="昵称"><br>
    <input type="number" name="age" placeholder="年龄"><br>
    <input id="but" type="button" disabled="disabled" value="注册" @click="reg()">
</form>
<!--引入axios框架-->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<!--引入vue框架-->
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
    let v = new Vue({
        el:"form",
        data:{
            info:"",
        },
        methods:{
           blurfn:function () {
                let data = new FormData(document.querySelector("form"));
                axios.post("/check",data).then(function (response) {
                    v.info=response.data;
                    let but = document.getElementById("but")
                    if ('该用户名可用!'==response.data){
                        but.disabled=false;
                    }else {
                        but.disabled=true;
                    }

                })
           },
            reg:function () {
                let data = new FormData(document.querySelector("form"));
                axios.post("/reg",data).then(function (response) {
                   //注册成功返回1,用户名已存在返回0
                    if(response.data==1){
                        alert("注册成功!")
                        //返回首页
                        location.href="/index.html";
                    }else {
                        alert("用户名已存在!")
                    }
                })
            }
        }
    });
</script>
</body>
</html>